from flask import Blueprint, Response, request
from relex.model import db, LexDocType
from relex.generators.lexdoctype import TGLexDocType
from relex.parsers.lexdoctype import FPLexDocType

lexdoctype_blue = Blueprint('lexdoctype', __name__)

@lexdoctype_blue.route('/api/1/lexdoctype', methods=['POST'])
def newLexDocType():
	doctype = FPLexDocType.parse(request.form)
	db.session.add(doctype)
	db.session.commit()
	return Response(unicode(doctype.id), status=201, mimetype='text/plain')

@lexdoctype_blue.route('/api/1/lexdoctype', methods=['GET'])
def getLexDocType():
	doctypes = LexDocType.query.all()
	generator = TGLexDocType(doctypes)
	return Response(generator.generate(), mimetype='text/plain')
